package com.threegroup.admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.threegroup.admin.dao.CategoryEntityMapper;
import com.threegroup.admin.entity.CategoryEntity;
import com.threegroup.common.dto.CategoryDTO;
import com.threegroup.common.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {
    @Resource
    private CategoryEntityMapper categoryEntityMapper;
    public Result<List<CategoryEntity>> queryList() {

        Result<List<CategoryEntity>> listResult = new Result<>();
        //查询数据库
        List<CategoryEntity> list =   categoryEntityMapper.queryAll();
        //设置结果集
        listResult.setData(list);
        return listResult;

    }

    public Result<PageInfo<CategoryDTO>> queryPage(Integer limit1, Integer page1, String order1, String orderField1, String name) {

        Result<PageInfo<CategoryDTO>> pageInfoResult = new Result<>();
        //校验参数
        Integer limit;
        Integer page;
        String order;
        String orderField;
        //校验并设置默认值
        limit = (limit1 != null) ? limit1 : 10;//默认10
        page = (page1 != null) ? page1 : 1;//默认第1页
        order = (StringUtils.isBlank(order1)) ? "asc" : order1;//默认
        orderField = (StringUtils.isBlank(orderField1)) ? "name" : orderField1;//默认排序字段
        //开启分页
        PageHelper.startPage(page, limit);
        //查询结果
        List<CategoryDTO> categoryDTOList = categoryEntityMapper.queryPageBy(order,orderField,name);
        //封装
        PageInfo<CategoryDTO> categoryDTOPageInfo = new PageInfo<>(categoryDTOList);
        pageInfoResult.setData(categoryDTOPageInfo);


        return  pageInfoResult;
    }
}
